function getQualificationInfo() {
    var tbody=window.document.getElementById("qualification_data");
    $.ajax({
        url:"../../qualification/seller",
        type:"GET",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="";
                var tdata=data.object;
                for( i in tdata){
                    var j = parseInt(i)+1;
                    str +="<tr>"+"" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ j +"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].industryName+"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].name+"</td>" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\"><label onclick='previewQualification("+ tdata[i].id +")'>点击查看资质</label></span> "+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ convertStateToString(tdata[i].state)+"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">" +
                        "<input type='button' value='编辑' id='edit_button'+" + tdata[i].id + " onclick='editButtonClick(" + tdata[i] + ")'" +"</input>"+
                        "<input type='button' value='删除' id='del_button'+" + tdata[i].id + " onclick='deleteButtonClick( " + tdata[i].id + ")'" +"</input></tr>";
                }
                tbody.innerHTML =str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });
}


function getAllQualificationInfo() {
    var tbody=window.document.getElementById("qualification_data");
    $.ajax({
        url:"../../qualification/seller/all",
        type:"GET",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="";
                var tdata=data.object;
                for( i in tdata){
                    var j = parseInt(i)+1;
                    str +="<tr>"+"" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ j +"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].sellerName+"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].industryName+"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].name+"</td>" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\"><label onclick='previewQualification("+ tdata[i].id +")'>点击查看资质</label></span> "+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ convertStateToString(tdata[i].state )+"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">";
                    if (tdata[i].state == 0) {
                        str += "<input type='button' value='通过' id='edit_button'+" + tdata[i].id + " onclick='editButtonClick(" + tdata[i] + ")'" +"</input>"+
                        "<input type='button' value='拒绝' id='del_button'+" + tdata[i].id + " onclick='deleteButtonClick( " + tdata[i].id + ")'" +"</input></tr>";
                    }
                    if (tdata[i].state == 1) {
                        str += "<input type='button' value='重审' id='del_button'+" + tdata[i].id + " onclick='deleteButtonClick( " + tdata[i].id + ")'" +"</input></tr>";
                    }

                }
                tbody.innerHTML =str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });

}

function convertStateToString(state) {
    if (state == 0) {
        return "未审核";
    }
    if (state == 1) {
        return "审核成功";
    }
}


function getIndustries() {
    var tbody=window.document.getElementById("_industry");
    $.ajax({
        url:"../../industry/all",
        type:"GET",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="<option value=''>选择行业</option>";
                var tdata=data.object;
                for( i in tdata) {
                    str += "<option value='" + tdata[i].id + "'>" + tdata[i].name + "</option>";
                }
                tbody.innerHTML =str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });
}

function industryLinkage() {
    $("#_industry").on("change",function(){
        getQualificationsByIndustry();
    });
}

function getQualificationsByIndustry() {
    var tbody=window.document.getElementById("_name");
    // 获取选中的行业
    var optionIndustry=$("#_industry option:selected");
    if (optionIndustry.val() == "") {
        return;
    } else {
        $.ajax({
            url:"../../qualification/industry",
            type:"POST",
            data:{industryId:optionIndustry.val()},
            async: false,
            contentType:"application/x-www-form-urlencoded; charset=utf-8",
            dataType:"json",
            success:function(data){
                console.log(data);
                if(data.code==0){
                    var str="";
                    var tdata=data.object;
                    for( i in tdata) {
                        str += "<option value='" + tdata[i].name + "'>" + tdata[i].name + "</option>";
                    }
                    tbody.innerHTML =str;
                }
            },
            error:function(){
                alert("search fails！");
            }
        });
    }

}

function addButtonClick() {
    location.href="./qualification_add.html";
}

function uploadSubmitButtonClick() {
    var formData = new FormData();
    formData.append('qualificationFile', $('#_qualification_image_file')[0].files[0]);
    formData.append('industry', $('#_industry').val());
    formData.append('name', $('#_name').val());
        $.ajax({
            url:"../../qualification/uploadimg",
            type:"POST",
            data: formData,
            dataType : 'json',
            cache: false,
            processData: false,
            contentType: false,
            success:function(result){
                if(result.code==0){
                    alert("上传成功");
                    document.getElementById("_url").value = result.object;
                }else if(result.code==1){
                    alert(result.message);
                }
            }
        // }).success(function (data) {
        //     alert(data);
        // }).error(function () {
        //     alert("上传失败");
        });
}

function addSubmitButtonClick() {
    var fdata = $("#qualification_add_form").serialize();
    $.param(fdata);
    $.ajax({
        url:"../../qualification/new",
        type:"POST",
        data:fdata,
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            if(result.code==0){
                alert(result.message);
                location.href="./qualification_list.html";
            }else if(result.code==1){
                alert(result.message);
            }
        }
    });
}


//      删除已选资质
function deleteButtonClick(id) {
    $.ajax({
        url:"../../qualification/deletion/" + id,
        type:"GET",
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            if(result.code==0){
                alert(result.message);
                location.href="./seller_qualification_list.html";
            }else if(result.code==1){
                alert(result.message);
            }
        }
    });
}

function previewQualification(id) {
    location.href="./seller_qualification_preview.html?id="+id;
}

function showImage() {
    var url = window.location.href;
    var id = getArgsFromHref(url, "id");
    document.getElementById("_qualification_image").setAttribute("src", "../../qualificationPreview?id="+id);
}


function getArgsFromHref(sHref, sArgName)
{
    var args = sHref.split("?");
    var retval = "";
    if(args[0] == sHref) /*参数为空*/
    {
        return retval; /*无需做任何处理*/
    }
    var str = args[1];
    args = str.split("&");
    for(var i = 0; i < args.length; i ++)
    {
        str = args[i];
        var arg = str.split("=");
        if(arg.length <= 1) continue;
        if(arg[0] == sArgName) retval = arg[1];
    }
    return retval;
}



